package fm.dongman.animefm.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import fm.dongman.animefm.R;

import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.MyContract;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.animefm.view.util.ActivityHelper;
import fm.dongman.animefm.view.util.ToastHelper;
import fm.dongman.modellib.model.CommentModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 首页最新评论适配器
 * Created by Administrator on 2017/10/10.
 */

public class HomeCommentAdapter extends RecyclerView.Adapter<HomeCommentAdapter.ViewHolder> {
    private static final int STEP_COUNT = 6;
    private List<IModel.ICommentModel> mListcommentModels;


    private Context mContext;
    private int mLoadCount = STEP_COUNT;

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView mAvatarUrl;       //用户头像
        private TextView mContent;          //内容
        private TextView mCreateTime;       //创建时间
        private TextView mUserName;         //用户名
        private TextView mCommentary;       //被评论作品名 ------暂无
        private View mCommentsView;         //评论view
        private View mSplitLine;

        public ViewHolder(View itemView) {
            super(itemView);

            mAvatarUrl = (ImageView) itemView.findViewById(R.id.mIvTheLatestCommentsPic);
            mContent = (TextView) itemView.findViewById(R.id.mTvTheLatestComments);
            mCreateTime = (TextView) itemView.findViewById(R.id.mTvTheLatestCommentsTime);
            mUserName = (TextView) itemView.findViewById(R.id.mTvTheLatestCommentsUserName);
            mCommentary = (TextView) itemView.findViewById(R.id.mTvProductionText1);
            mSplitLine = itemView.findViewById(R.id.mViewCommentLine);
            mCommentsView = itemView;
        }
    }

    public HomeCommentAdapter(List<IModel.ICommentModel> list, Context mContext) {
        this.mListcommentModels = list;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_the_latest_comments, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final CommentModel comment = (CommentModel) mListcommentModels.get(position);
        Glide.with(mContext)
                .load(comment.getUser().getAvatarUrl())
                .bitmapTransform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 18))
                .placeholder(R.mipmap.avatar)
                .into(holder.mAvatarUrl);
        holder.mContent.setText(comment.getContent());
        holder.mCreateTime.setText(comment.getCreateTime());
        holder.mUserName.setText(comment.getUser().getUserName());
        holder.mCommentary.setText(comment.getTitle());

        //来源textView事件
        holder.mCommentary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SingleActivity.class);
                intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_ANIME_INFO());
                intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), comment.getSubjectId());
                ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
            }
        });

        holder.mAvatarUrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //   点击头像进入用户详细信息界面
                if (!isLogin()) {
                    Intent intent = new Intent(mContext, SingleActivity.class);
                    intent.putExtra(SingleActivity.Companion.getEXTRA_NAME(), SingleActivity.Companion.getINTENT_EXTRA_LOGIN());
                    ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                    return;
                }
                Intent intent = new Intent(mContext, SingleActivity.class);
                intent.putExtra(SingleActivity.Companion.getEXTRA_NAME(), SingleActivity.Companion.getINTENT_EXTRA_USER_INFO());
                intent.putExtra(SingleActivity.Companion.getBUNDLE_EXTRA_KEY_ID(), comment.getUser().getUserId());
                mContext.startActivity(intent);
            }
        });


        //View项单击事件
        holder.mCommentsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入评论页面
//                Intent intent = new Intent(mContext, SingleActivity.class);
//                intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_COMMENT_LIST());
//                ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
            }
        });
    }

    /**
     * 如果用户没有登录跳转登录界面
     */
    private static boolean isLogin() {
        return MyContract.Companion.getUSER_ID() != null;
    }

    @Override
    public int getItemCount() {
        if (mListcommentModels.size() < mLoadCount) {
            mLoadCount = mListcommentModels.size();
        }
        return mLoadCount;
    }

    /**
     * 分流加载更多
     *
     * @return 是否有更多数据
     */
    public boolean loadMore() {
        mLoadCount += STEP_COUNT;
        if (mLoadCount > mListcommentModels.size()) {
            mLoadCount = mListcommentModels.size();
            notifyDataSetChanged();
            return false;
        }
        notifyDataSetChanged();
        return true;
    }

}
